<?php

	class EbFilesToolkit
	{
		static public function getExtension($file)
		{
			if (strpos($file,'.') > 0)
			{
				$buffer = explode(".",$file);
				return strtoupper(end($buffer));
			}
			else
			{
				return '';
			}
		}
		
		static public function formatFileSize($bytes,$precision = 2)
		{
			$units = array('o', 'Ko', 'Mo', 'Go', 'To'); 
   
		    $bytes = max($bytes, 0); 
		    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
		    $pow = min($pow, count($units) - 1); 
		   
		    $bytes /= pow(1024, $pow); 
		   
		    return round($bytes, $precision) . ' ' . $units[$pow]; 
		}
		
		static public function getFile($value)
		{
			$buffer = explode("/",$value);
			return end($buffer);
		}
		
		static public function getFilename($value)
		{
			$value = self::getFile($value);
			$buffer = explode(".",$value);
			return $buffer[0];
		}		
		
		static public function getFiles($path, $pattern) 
		{
			$result = array();
			$path = rtrim(str_replace("\\", "/", $path), '/') . '/';
			$entries = Array();
			$dir = dir($path);
					
			while (false !== ($entry = $dir->read())) 
			{
				$entries[] = $entry;
			}
			
			$dir->close();
			
			foreach ($entries as $entry) 
			{
				$fullname = $path . $entry;
				
				if ($entry != '.' && $entry != '..' && is_dir($fullname)) 
				{
					$buffer = self::getFiles($fullname, $pattern);
					
					foreach ($buffer AS $item)
					{
						$result []= $item;
					}
				}
				else if (is_file($fullname) && preg_match($pattern,$fullname)) 
				{
					$result []= $fullname;
				}
			}
			
			return $result;
		}
	}